import axios from 'axios'
import config from './config.js'

const instance = axios.create({
  baseURL: config.axiosBaseURL,
  timeout: 10000,
})

instance.interceptors.response.use(
  (response) => {
    if (response.status >= 200 && response.status < 300) {
      return response
    } else {
      console.warn('Request succeeded with non-2xx status:', response.status)
      return Promise.reject(response)
    }
  },
  (error) => {
    if (error.response) {
      console.error('Error status:', error.response.status)
      console.error('Error data:', error.response.data)
    } else if (error.request) {
      console.error('No response received:', error.request)
    } else {
      console.error('Error message:', error.message)
    }
    return Promise.reject(new Error('The request failed, please try again later.'))
  }
)

export default instance
